﻿using System;
using System.Collections.Generic;
using System.Linq;

using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Enum;
using Kugar.MVC.MobileUI.WeUI.Helpers;

#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif


namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons
{
    public class PageCommon_Cells:ContainerBase
    {
        private string _title = "";

        public PageCommon_Cells(ViewContext viewContext) : base(viewContext)
        {
            this.Css("weui-cells");
        }

        public PageCommon_Cells Title(string title)
        {
            _title = title;

            return this;
        }

        public Cell RenderCell()
        {
            return new Cell(this,this.ViewContext);
        }

        public CellContainer RenderCellContainer()
        {
            return new CellContainer(ViewContext);
        }

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</div>");
        }

        internal override ContainerBase RenderInternal()
        {
            var id = _attrs.Attributes.TryGetValue("id","cells_" +  base.WeUI.NextControlID());

            if (!string.IsNullOrWhiteSpace(_title))
            {
                ViewContext.Writer.WriteLine($"<div class=\"weui-cells__title\" for=\"{id}\">{_title}</div>");
            }

            _attrs.ID(id);
            
            ViewContext.Writer.WriteLine($"<div {_attrs}>");

            return this;
        }

        public class Cell:ControlBase
        {
            private string _header = "";
            private string _body = "";
            private string _footer = "";

            public Cell(PageCommon_Cells parentCells, ViewContext viewContext) : base(viewContext)
            {
                this.Css("weui-cell");
            }

            public Cell Header(Func<Cell, HelperResult> helper)
            {
                _header = helper(this).ToHtmlString();

                return this;
            }

            public Cell Header(string header)
            {
                _header = header;

                return this;
            }

            public Cell Body(Func<Cell, HelperResult> helper)
            {
                _body = helper(this).ToHtmlString();
                return this;
            }

            public Cell Body(string body)
            {
                _body = body;
                return this;
            }

            public Cell Footer(Func<Cell, HelperResult> helper)
            {
                _footer = helper(this).ToHtmlString();
                return this;
            }

            public Cell Footer(string footer)
            {
                _footer = footer;
                return this;
            }

            public override MvcHtmlString Render()
            {
                ViewContext.Writer.WriteLine($"<div {_attrs}>");

                if (!string.IsNullOrWhiteSpace(_header))
                {
                    ViewContext.Writer.WriteLine("<div class=\"weui-cell__hd\"><label class=\"weui-label\">");

                    ViewContext.Writer.WriteLine(_header);

                    ViewContext.Writer.WriteLine("</label></div>");

                }

                if (!string.IsNullOrWhiteSpace(_body))
                {
                    ViewContext.Writer.WriteLine("<div class=\"weui-cell__bd\">");

                    ViewContext.Writer.WriteLine(_body);

                    ViewContext.Writer.WriteLine("</div>");

                }

                if (!string.IsNullOrWhiteSpace(_footer))
                {
                    ViewContext.Writer.WriteLine("<div class=\"weui-cell__ft\">");

                    ViewContext.Writer.WriteLine(_footer);

                    ViewContext.Writer.WriteLine("</div>");

                }

                ViewContext.Writer.WriteLine("</div> <!-- end weui-cell -->");

                return MvcHtmlString.Empty;
            }
        }

        public class CellContainer:ContainerBase
        {
            public ContainerBase Body()
            {
                return new CellBody(ViewContext);
            }

            public ContainerBase Header()
            {
                return new CellHeader(ViewContext);
            }

            public ContainerBase Footer()
            {
                return new CellFooter(ViewContext);
            }

            private class CellHeader : ContainerBase
            {
                public CellHeader(ViewContext viewContext) : base(viewContext)
                {
                    this.Css("weui-cell__hd");
                }

                public override void Dispose()
                {
                    ViewContext.Writer.WriteLine("</div> <!-- end  cell__hd-->");
                }

                internal override ContainerBase RenderInternal()
                {
                    ViewContext.Writer.WriteLine($"<div {_attrs}>");

                    return this;
                }
            }

            private class CellBody : ContainerBase
            {
                public CellBody(ViewContext viewContext) : base(viewContext)
                {
                    this.Css("weui-cell__bd");
                }

                public override void Dispose()
                {
                    ViewContext.Writer.WriteLine("</div> <!-- end  cell__bd-->");
                }

                internal override ContainerBase RenderInternal()
                {
                    ViewContext.Writer.WriteLine($"<div {_attrs}>");

                    return this;
                }
            }

            private class CellFooter : ContainerBase
            {
                public CellFooter(ViewContext viewContext) : base(viewContext)
                {
                    this.Css("weui-cell__ft");
                }

                public override void Dispose()
                {
                    ViewContext.Writer.WriteLine("</div> <!-- end  cell__ft-->");
                }

                internal override ContainerBase RenderInternal()
                {
                    ViewContext.Writer.WriteLine($"<div {_attrs}>");

                    return this;
                }
            }

            public CellContainer(ViewContext viewContext) : base(viewContext)
            {
            }

            public override void Dispose()
            {
                ViewContext.Writer.WriteLine("</div> <!-- end weui-cell -->");
            }

            internal override ContainerBase RenderInternal()
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-cell\">");

                return this;
            }
        }
    }

    public class PageCommon_Cell:ContainerBase
    {
        private bool _swiped = false;
        private string _iconUrl = "";
        private Lazy<List<SwipeButton>> _swipeLst=new Lazy<List<SwipeButton>>(() => new List<SwipeButton>(2));

        public PageCommon_Cell(ViewContext viewContext) : base(viewContext)
        {
        }

        public virtual PageCommon_Cell Icon(string iconUrl)
        {
            _iconUrl = iconUrl;

            return this;
        }

        public virtual PageCommon_Cell AddSwipeButton(Action<SwipeButton> config)
        {
            var cof=new SwipeButton();
            
            config(cof);

            _swipeLst.Value.Add(cof);

            _swiped = true;

            return this;
        }

        public ContainerBase RenderBody()
        {
            return new CellBody(ViewContext);
        }

        public ContainerBase RenderFooter()
        {
            return new CellFooter(ViewContext);
        }

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</div> <!-- end weui-cell -->");

            if (_swiped)
            {
                ViewContext.Writer.WriteLine("</div> <!-- end swiped cell__bd -->");

                ViewContext.Writer.WriteLine("<div class=\"weui-cell__ft\">");

                foreach (var btn in _swipeLst.Value)
                {
                    ViewContext.Writer.WriteLine(btn.RenderButton());
                }

                ViewContext.Writer.WriteLine("</div> <!-- end swiped cell__ft -->");

                ViewContext.Writer.WriteLine("</div> <!-- end cell_swiped  -->");
            }
        }


        internal override ContainerBase RenderInternal()
        {
            if (_swiped)
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-cell weui-cell_swiped\">");

                ViewContext.Writer.WriteLine("<div class=\"weui-cell__bd\">");
            }

            ViewContext.Writer.WriteLine("<div class=\"weui-cell\">");

            if (!string.IsNullOrWhiteSpace(_iconUrl))
            {
                ViewContext.Writer.WriteLine($"<div class=\"weui-cell__hd\"><img src=\"{_iconUrl}\" alt=\"\" style=\"width:20px;margin-right:5px;display:block\"></div>");
            }
            

            return this;
        }

        private class CellBody : ContainerBase
        {
            public CellBody(ViewContext viewContext) : base(viewContext)
            {
            }

            public override void Dispose()
            {
                ViewContext.Writer.WriteLine("</div> <!-- end  cell__bd-->");
            }

            internal override ContainerBase RenderInternal()
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-cell__bd\">");

                return this;
            }
        }

        private class CellFooter : ContainerBase
        {
            public CellFooter(ViewContext viewContext) : base(viewContext)
            {
            }

            public override void Dispose()
            {
                ViewContext.Writer.WriteLine("</div> <!-- end  cell__ft-->");
            }

            internal override ContainerBase RenderInternal()
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-cell__ft\">");

                return this;
            }
        }
    }



    public class SwipeButton:ButtonItemBase
    {
        private bool _isCloseSwipe = false;
        private bool _isDeleteSwipe = false;

        public SwipeButton()
        {
            this.ButtonStyle(ButtonStyleType.Default);
        }

        public SwipeButton CloseSwip()
        {
            this.Class("close-swipeout");

            return this;
        }

        public SwipeButton DeleteSwipe()
        {
            this.Class("delete-swipeout");

            return this;
        }

        public virtual SwipeButton ButtonStyle(ButtonStyleType style)
        {
            base.ButtonStyle(style);

            return this;
        }

        public virtual SwipeButton OnClick(string script)
        {
            base.OnClick(script);

            return this;
        }

        public new SwipeButton Text(string text)
        {
            base.Text(text);

            return this;
        }

        public new SwipeButton Href(string gotoUrl)
        {
            base.Href(gotoUrl);

            return this;
        }

        public new SwipeButton Type(ButtonTypeEnum type)
        {
            base.Type(type);

            return this;
        }

        public new SwipeButton ID(string id)
        {
            base.ID(id);

            return this;
        }

        public new SwipeButton Class(string css)
        {
            base.Class(css);

            return this;
        }

        public new SwipeButton Data(string dataName, string value)
        {
            base.Data(dataName, value);

            return this;
        }

        internal string RenderButton()
        {
            if (getType== ButtonTypeEnum.Submit)
            {
                _clickScript =_clickScript + ";WebUIJS.Submit(this);";
            }

            return $"<a class=\"{ButtonStyleToStr(getButtonStyle)} {getClass}\" {(getHref.IsNotNullOrEmpty() ? $"href=\"{getHref}\"" : "")} {(getOnClickScript.IsNotNullOrEmpty() ? $"onclick=\"{getOnClickScript}\"" : "")} id=\"{getID}\" type=\"{Utility.ButtonTypeToStr(getType)}\">{getText}</a>";
        }

        private static string ButtonStyleToStr(ButtonStyleType type)
        {
            switch (type)
            {
                case ButtonStyleType.Default:
                {
                    return "weui-swiped-btn weui-swiped-btn_default";
                }
                case ButtonStyleType.Primary:
                {
                    return "weui-swiped-btn weui-swiped-btn_primary";
                }
                case ButtonStyleType.Success:
                {
                    return "weui-swiped-btn weui-swiped-btn_success";
                }
                case ButtonStyleType.Info:
                {
                    return "weui-swiped-btn weui-swiped-btn_info";
                }
                case ButtonStyleType.Warning:
                {
                    return "weui-swiped-btn weui-swiped-btn_warn";
                }
                case ButtonStyleType.Danger:
                {
                    return "weui-swiped-btn weui-swiped-btn_danger";
                }
                case ButtonStyleType.Link:
                {
                    return "weui-swiped-btn weui-swiped-btn_link";
                }
                case ButtonStyleType.Lazur:
                {
                    return "weui-swiped-btn weui-swiped-btn_lazur";
                }
            }

            return "";
        }
    }
}